package 耿翔宇;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.IOUtils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ReUtil;

public class PZ {

	public static void main(String[] args) throws MalformedURLException, IOException {
		//Ů����
		InputStream gs = new URL( "http://mingzi.jb51.net/qiming/nvhai/68815554142.html" ).openStream();
		String regex = "(..��){4}..";
		String girl = IoUtil.read(gs, "utf-8");
		List<String> findGirls = ReUtil.findAll(regex, girl, 0);	
		List<String> girlNames = execDataMing(findGirls);
//		System.out.println(girlNames);
		IOUtils.closeQuietly(gs);
		//������
		InputStream ns = new URL( "http://mingzi.jb51.net/qiming/nanhai/68814860866.html" ).openStream();
		String boy = IoUtil.read(ns, "utf-8");
		List<String> findBoys = ReUtil.findAll(regex, boy, 0);	
		List<String> boyNames = execDataMing(findBoys);
//		System.out.println(boyNames);
		IOUtils.closeQuietly(ns);
		//��
		InputStream surname = new URL( "https://jingyan.baidu.com/article/2f9b480dea184b00cb6cc2d4.html" ).openStream();
		String sn = IoUtil.read(surname, "utf-8");
		String regex2 = "(.��){99}.";
		List<String> findSurname = ReUtil.findAll(regex2, sn, 0);
		List<String> surnames = execDataXing(findSurname);
//		System.out.println(surnames);
		IOUtils.closeQuietly(surname);
		//������
		ArrayList<String> girlInfo = getInfo(surnames, girlNames, "Ů");
		ArrayList<String> boyInfo = getInfo(surnames, boyNames, "��");
//		System.out.println(girlInfo);
//		System.out.println(boyInfo);
		File file1 = new File("girls.txt");
		File file2 = new File("boys.txt");
		FileUtil.writeLines(girlInfo, file1, "utf-8",true);
		FileUtil.writeLines(boyInfo, file2, "utf-8",true);
	}
	
	private static ArrayList<String> getInfo(List<String> xing, List<String> ming, String sex) {
		HashSet<String> set=new HashSet<>();
		while(true){
			if(set.size()==50){
				break;
			}
			Collections.shuffle(xing);
			Collections.shuffle(ming);
			String info = xing.get(0)+ming.get(0)+"-"+sex+"-"+(new Random().nextInt(7)+18);
			set.add(info);
		}
		return new ArrayList(set);
	}
	
	private static List<String> execDataMing(List<String> listMing) {
		List<String>  ming=new ArrayList<>();
		for (String string : listMing) {
			String[] split = string.split("��");
			for (String string2 : split) {
				ming.add(string2);
			}
		}
		return ming;
	}

	private static List<String> execDataXing(List<String> listXing) {
		ArrayList<String>  xing=new ArrayList<>();
		String[] split = listXing.get(0).split("��");
		for(String s : split){
			xing.add(s);
		}
		return xing;
	}
}
